﻿using System;
using System.Collections.Generic;
using SimpleServiceBus.EndpointMonitor.Web.Helpers.Flot.FlotOptions;

namespace SimpleServiceBus.EndpointMonitor.Web.Helpers.Flot
{
    public class FlotChartViewData
    {

        public FlotChartViewData()
        {
            ChartDivId = "flotchart";
            Width = "100%";
            Height = "300px";
            ShowTitle = true;
            GlobalOptions = new FlotGlobalOptions();
            if (FlotChartDefaults.Global != null)
                FlotChartDefaults.Global(this);

            MaxAxisLabelLength = 100;
        }

        public virtual string DataUrl { get; set; }
        public string ChartDivId { get; set; }
        public string CssClass { get; set; }
        public virtual string Title { get; set; }
        public bool ShowTitle { get; set; }
        public Dictionary<string, FlotSeriesOptions> SeriesOptions { get; set; }
        public FlotGlobalOptions GlobalOptions { get; private set; }
        public string Width { get; set; }
        public string Height { get; set; }
        public bool Pivot { get; set; }
        public bool ReturnCaptions { get; set; }
        public bool ReturnIdentifiers { get; set; }
        public Axes DiscreteAxis { get; set; }
        public int MaxAxisLabelLength { get; set; }

        public Action<FlotChartViewData>[] Defaults
        {
            set
            {
                if (value != null && value.Length > 0)
                {
                    foreach (var def in value)
                    {
                        def(this);
                    }
                }
            }
        }

        public FlotChartViewData ConfigureGlobalOptions(Action<FlotGlobalOptions> config)
        {
            if (config != null)
                config(GlobalOptions);
            return this;
        }

        public FlotChartViewData AddNewSeriesOptions(string seriesName, FlotSeriesOptions options)
        {
            if (SeriesOptions == null)
                SeriesOptions = new Dictionary<string, FlotSeriesOptions>();
            SeriesOptions[seriesName] = options;
            return this;
        }

        public FlotChartViewData AddNewSeriesOptions(string seriesName, Action<FlotSeriesOptions> configure)
        {
            var options = new FlotSeriesOptions();
            AddNewSeriesOptions(seriesName, options);
            if (configure != null)
                configure(options);
            return this;
        }

        public bool HasPlotOptions
        {
            get
            {
                return GlobalOptions != null;
            }
        }

    }
}